;; EX-09.scm - Scheme level sound file shuffler

;; Exercise the client schedule and the server node allocator.  This
;; is a translation of a post by James McCartney to sc-users, Mon, 26
;; May 2003 15:13:35 -0700

(synthdef-send
 (synthdef
  "grn"
  ((bufnum 0))
  (Out.ar 0 
	  (Pan2.ar 
	   (PlayBuf.ar 1 
		       bufnum
		       (Mul (Mul (MouseY.kr 4 0.25 1) (Rand.ir 0.95 1.05)) 
			    (BufRateScale.kr bufnum))
		       1
		       (Mul (Add (MouseX.kr 0 1) (Rand.ir -0.05 0.05)) 
			    (BufSamples.kr bufnum))
		       1)
	   (IRand.ir -1 1)
	   (EnvGen.kr envelope: (Env '(0 0.8 0) '(0.05 0.05)) 
		      gate: 1 
		      doneAction: 2)))))

(begin 
  (define b (bID))
  (-> "/b_allocRead" b (resolve "audio/metal.wav") 0 -1))

(define t (make-tschedule))

(at t 
    (lambda (pulse utc)
      (=> utc '("/s_new" "grn" -1 1 1 "bufnum" b))
      1/50))

(set-tschedule-ppm! t 60)

(tschedule-clear! t)

(begin (-> "/b_free" b)
       (bID-release b))

;; As required...

(set-server-latency! 0.75)
(schedule-start)
